package com.zd.controller;

import com.github.pagehelper.PageInfo;
import com.zd.model.InfoManage;
import com.zd.model.LayuiFile;
import com.zd.model.UserInfo;
import com.zd.service.IInfoManageService;
import com.zd.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/infoManage")
public class InfoManageController extends BaseController {

    //读取配置文件获取上传文件夹路径
    @Value("${upload.uploadPath}")
    private String uploadPath;


    @Autowired
    private IInfoManageService infoManageService;

    /**
     * 信息发布展示页
     *
     * @param
     * @param
     * @param queryMap
     * @param model
     * @return
     */
    /*Map<String,Object> policyMap,Map<String,Object> schoolMap,*/
    @RequestMapping(value = "/indexTotal", method = {RequestMethod.POST, RequestMethod.GET})
    public String indexTotal(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                     int pageNum,
                             @RequestParam(name = "pageSize", required = false, defaultValue = "5")
                                     int pageSize, @RequestParam Map<String, Object> queryMap, Model model) {

        List<InfoManage> selectSchoolList = infoManageService.selectSchoolManage(pageNum, pageSize, queryMap);
        List<InfoManage> selectPolicyDocumentList = infoManageService.selectPolicyDocumentManage(pageNum, pageSize, queryMap);
        List<InfoManage> infoManageList = infoManageService.selectInfo(pageNum, pageSize, queryMap);

        model.addAttribute("pageInfo", new PageInfo<>(infoManageList));
        model.addAttribute("infoManageList", infoManageList);
        model.addAttribute("selectSchoolList", selectSchoolList);
        model.addAttribute("selectPolicyDocumentList", selectPolicyDocumentList);
        model.addAttribute("queryMap", queryMap);
        return "infoTotalIndex";
    }


    @RequestMapping(value = "/infoMore", method = {RequestMethod.POST, RequestMethod.GET})
    public String infoIndex(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                    int pageNum,
                            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                                    int pageSize, @RequestParam Map<String, Object> queryMap, Model model) {
        //查询信息发布类信息
        queryMap.put("type", 1);
        List<InfoManage> infoManageList = infoManageService.selectInfoManage(pageNum, pageSize, queryMap);

        model.addAttribute("pageInfo", new PageInfo<>(infoManageList));
        model.addAttribute("infoManageList", infoManageList);
        model.addAttribute("queryMap", queryMap);
        return "infoMore";
    }

    @RequestMapping(value = "/schoolEnterpriseMore", method = {RequestMethod.POST, RequestMethod.GET})
    public String schoolEnterpriseMore(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                               int pageNum,
                                       @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                                               int pageSize, @RequestParam Map<String, Object> queryMap, Model model) {
        //查询信息发布类信息
        queryMap.put("type", 3);
        List<InfoManage> infoManageList = infoManageService.selectInfoManage(pageNum, pageSize, queryMap);

        model.addAttribute("pageInfo", new PageInfo<>(infoManageList));
        model.addAttribute("infoManageList", infoManageList);
        model.addAttribute("queryMap", queryMap);
        return "schoolEnterpriseMore";
    }

    @RequestMapping(value = "/policyDocumentMore", method = {RequestMethod.POST, RequestMethod.GET})
    public String policyDocumentMore(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                             int pageNum,
                                     @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                                             int pageSize, @RequestParam Map<String, Object> queryMap, Model model) {
        //查询信息发布类信息
        queryMap.put("type", 2);
        List<InfoManage> infoManageList = infoManageService.selectInfoManage(pageNum, pageSize, queryMap);

        model.addAttribute("pageInfo", new PageInfo<>(infoManageList));
        model.addAttribute("infoManageList", infoManageList);
        model.addAttribute("queryMap", queryMap);
        return "policyDocumentMore";
    }


    /**
     * 校企活动主页
     *
     * @param pageNum
     * @param pageSize
     * @param queryMap
     * @param model
     * @return
     */
    @RequestMapping(value = "/schoolEnterpriseActivities", method = {RequestMethod.POST, RequestMethod.GET})
    public String schoolEnterpriseActivitiesIndex(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                                          int pageNum,
                                                  @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                                                          int pageSize, @RequestParam Map<String, Object> queryMap, Model model) {

        //查询信息发布类信息
        queryMap.put("type", 3);

        List<InfoManage> infoManageList = infoManageService.selectInfoManage(pageNum, pageSize, queryMap);

        model.addAttribute("pageInfo", new PageInfo<>(infoManageList));
        model.addAttribute("infoManageList", infoManageList);
        model.addAttribute("queryMap", queryMap);

        return "schoolEnterpriseActivitiesIndex";
    }

    /**
     * 政策文件主页
     *
     * @param pageNum
     * @param pageSize
     * @param queryMap
     * @param model
     * @return
     */
    @RequestMapping(value = "/policyDocument", method = {RequestMethod.POST, RequestMethod.GET})
    public String policyDocumentIndex(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                              int pageNum,
                                      @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                                              int pageSize, @RequestParam Map<String, Object> queryMap, Model model) {

        //查询信息发布类信息
        queryMap.put("type", 2);

        List<InfoManage> infoManageList = infoManageService.selectInfoManage(pageNum, pageSize, queryMap);

        model.addAttribute("pageInfo", new PageInfo<>(infoManageList));
        model.addAttribute("infoManageList", infoManageList);
        model.addAttribute("queryMap", queryMap);

        return "policyDocumentIndex";
    }

    /**
     * 信息发布主页
     *
     * @param pageNum
     * @param pageSize
     * @param queryMap
     * @param model
     * @return
     */
    @RequestMapping(value = "/queryInfoManage", method = {RequestMethod.POST, RequestMethod.GET})
    public String queryInfoManage(@RequestParam(name = "pageNum", required = false, defaultValue = "1")
                                          int pageNum,
                                  @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                                          int pageSize, @RequestParam Map<String, Object> queryMap, Model model) {

        //查询信息发布类信息
        queryMap.put("type", 1);

        List<InfoManage> infoManageList = infoManageService.selectInfoManage(pageNum, pageSize, queryMap);

        model.addAttribute("pageInfo", new PageInfo<>(infoManageList));
        model.addAttribute("infoManageList", infoManageList);
        model.addAttribute("queryMap", queryMap);


        return "infoManageIndex";
    }


    /**
     * 跳转添加页面
     *
     * @return
     */
    @GetMapping("toInfoManageAdd")
    public String toInfoManageAdd() {
        return "infoManageAdd";
    }

    /**
     * 跳转添加校企活动页面
     *
     * @return
     */
    @GetMapping("toSchoolEnterpriseActivitiesAdd")
    public String toSchoolEnterpriseActivitiesAdd() {
        return "schoolEnterpriseActivitiesAdd";
    }

    /**
     * 跳转添加政策文件页面
     *
     * @return
     */
    @GetMapping("toPolicyDocumentAdd")
    public String toPolicyDocumentAdd() {
        return "policyDocumentAdd";
    }


    /**
     * 插入校企活动信息
     *
     * @param infoManage
     * @return
     */
    @ResponseBody
    @PostMapping("schoolEnterpriseActivitiesAdd")
    public boolean schoolEnterpriseActivitiesAdd(InfoManage infoManage) {

        //入库时间
        infoManage.setEntertime(new Date());

        //录入用户id
        HttpSession session = getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");

        infoManage.setEnteruserid(userInfo.getId());

        //类型为发布信息 3
        infoManage.setType(3);

        infoManageService.insertSelective(infoManage);

        return true;
    }

    /**
     * 插入政策文件信息
     *
     * @param infoManage
     * @return
     */
    @ResponseBody
    @PostMapping("policyDocumentAdd")
    public boolean policyDocumentAdd(InfoManage infoManage) {
        //获取多协议文件
        List<MultipartFile> fileUploadList = infoManage.getFileUpload();

        if (fileUploadList != null && fileUploadList.size() != 0) {

            //文件上传
            String uploadNewFileName = FileUtil.uploadFile(fileUploadList, uploadPath);

            //存入数据库上传文件名
            infoManage.setFileurl(uploadNewFileName);
        }
        //入库时间
        infoManage.setEntertime(new Date());

        //录入用户id
        HttpSession session = getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");

        infoManage.setEnteruserid(userInfo.getId());

        //类型为发布信息 2
        infoManage.setType(2);

        infoManageService.insertSelective(infoManage);

        return true;
    }


    /**
     * Layui富文本框图片上传
     *
     * @param file
     * @param domain
     * @return Layui富文本框返回Json字符串
     */
    @ResponseBody
    @PostMapping("uploadLayuiFile")
    public LayuiFile uploadLayuiFile(MultipartFile file, String domain) {

        //上传获取到文件名
        String fileName = FileUtil.uploadFileSingle(file, uploadPath);

        LayuiFile layuiFile = new LayuiFile();
        //上传成功
        layuiFile.setCode(0);

        LayuiFile.LayuiFileData data = new LayuiFile.LayuiFileData();

        data.setSrc(domain + "/staticFile/" + fileName);
        data.setTitle(file.getOriginalFilename());

        layuiFile.setData(data);


        return layuiFile;
    }

    /**
     * 插入信息
     *
     * @param infoManage
     * @return
     */
    @ResponseBody
    @PostMapping("infoManageAdd")
    public boolean infoManageAdd(InfoManage infoManage) {

        //入库时间
        infoManage.setEntertime(new Date());

        //录入用户id
        HttpSession session = getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");

        infoManage.setEnteruserid(userInfo.getId());

        //类型为发布信息 1
        infoManage.setType(1);

        infoManageService.insertSelective(infoManage);

        return true;
    }

    /**
     * 跳转修改页
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("toInfoManageUpdate")
    public String toInfoManageUpdate(int id, Model model) {

        InfoManage infoManage = infoManageService.selectInfoManageById(id);

        model.addAttribute("infoManage", infoManage);

        return "infoManageUpdate";
    }

    /**
     * 跳转校企活动修改页
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("toSchoolEnterpriseActivitiesUpdate")
    public String toSchoolEnterpriseActivitiesUpdate(int id, Model model) {

        InfoManage infoManage = infoManageService.selectInfoManageById(id);

        model.addAttribute("infoManage", infoManage);

        return "schoolEnterpriseActivitiesUpdate";
    }

    /**
     * 跳转政策文件修改页
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("toPolicyDocumentUpdate")
    public String toPolicyDocumentUpdate(int id, Model model) {

        InfoManage infoManage = infoManageService.selectInfoManageById(id);

        model.addAttribute("infoManage", infoManage);

        return "policyDocumentUpdate";
    }

    /**
     * 修改信息
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/infoManageUpdate")
    public boolean InfoManageUpdate(InfoManage infoManage) {
        infoManageService.updateSelective(infoManage);
        return true;
    }

    /**
     * 修改政策文件
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/policyDocumentUpdate")
    public boolean PolicyDocumentUpdate(InfoManage infoManage) {
        //获取多协议文件
        List<MultipartFile> fileUploadList = infoManage.getFileUpload();

        if (fileUploadList != null && fileUploadList.size() != 0) {

            //文件上传
            String uploadNewFileName = FileUtil.uploadFile(fileUploadList, uploadPath);

            //存入数据库上传文件名
            infoManage.setFileurl(uploadNewFileName);
        }
        infoManageService.updateSelective(infoManage);
        return true;
    }


    /**
     * 查看详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("toInfoManageDetail")
    public String toInfoManageDetail(int id, Model model) {

        InfoManage infoManage = infoManageService.selectInfoManageById(id);

        model.addAttribute("infoManage", infoManage);

        return "infoManageDetail";
    }

    /**
     * 查看政策文件详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("toPolicyDocumentDetail")
    public String toPolicyDocumentDetial(int id, Model model) {

        InfoManage infoManage = infoManageService.selectInfoManageById(id);

        model.addAttribute("infoManage", infoManage);

        return "policyDocumentDetail";
    }

    /**
     * 下载文件
     *
     * @param response
     * @param fileName
     */
    @GetMapping("downloadFile")
    public void downloadFile(HttpServletResponse response, String fileName) {

        FileUtil.downloadFile(response, uploadPath, fileName);


    }

    /**
     * 删除政策文件
     *
     * @return
     */
    @ResponseBody
    @GetMapping("deleteFileurlName")
    public boolean deleteFileurlName(int id, String deleteFileurlName) {

        infoManageService.deleteFileurlName(id, deleteFileurlName);

        return true;

    }

    /**
     * 删除信息发布
     *
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("deleteInfoManage")
    public boolean deleteInfoManage(int id) {

        infoManageService.deleteInfoManage(id);

        return true;
    }
}
